﻿Option Explicit On
Option Strict On


Imports System.ServiceModel
Imports System.Runtime.Serialization

Namespace Framework.Hardware

    <Serializable()> _
    <DataContract()> _
    Public Class BandSegment

#Region " Shared Members "

        Private Shared s_Segments As Generic.Dictionary(Of Integer, BandSegment)

#End Region

#Region " Private Members "

        Private m_SegmentID As Integer
        Private m_LowerFrequencyKhz As Integer
        Private m_UpperFrequencyKhz As Integer

#End Region

#Region " Shared Constructor "

        ''' <summary>
        ''' Shared Constructor 
        ''' </summary>
        ''' <remarks></remarks>
        Shared Sub New()
            GenerateSegments()
        End Sub

#End Region

#Region " Constructor "

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <param name="pSegmentID">The ID of the segment</param>
        ''' <param name="pLowerFrequency">The Lower Frequency in Khz</param>
        ''' <param name="pUpperFrequency">The Upper Frequency in Khz</param>
        ''' <remarks></remarks>
        Private Sub New(ByVal pSegmentID As Integer, ByVal pLowerFrequency As Integer, ByVal pUpperFrequency As Integer)
            m_SegmentID = pSegmentID
            m_LowerFrequencyKhz = pLowerFrequency
            m_UpperFrequencyKhz = pUpperFrequency
        End Sub

#End Region

#Region " Public Properties "

        ''' <summary>
        ''' The ID of the segment
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DataMember()> _
        Public Property SegmentID As Integer
            Get
                Return m_SegmentID
            End Get
            Set(value As Integer)
                m_SegmentID = value
            End Set
        End Property

        ''' <summary>
        ''' The Lower Frequency of the Segment in Khz
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DataMember()> _
        Public Property LowerFrequency As Integer
            Get
                Return m_LowerFrequencyKhz
            End Get
            Set(value As Integer)
                m_LowerFrequencyKhz = value
            End Set
        End Property

        ''' <summary>
        ''' The Upper Frequency of the Segment in Khz
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DataMember()> _
        Public Property UpperFrequency As Integer
            Get
                Return m_UpperFrequencyKhz
            End Get
            Set(value As Integer)
                m_UpperFrequencyKhz = value
            End Set
        End Property

#End Region

#Region " Public Shared Methods "

        ''' <summary>
        ''' Returns the BandSegment for a given ID
        ''' </summary>
        ''' <param name="ID">The ID of the segment to return</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetSegment(ByVal ID As Integer) As BandSegment
            Return s_Segments.Item(ID)
        End Function

        ''' <summary>
        ''' Returns the BandSegment that contains the specified frequency
        ''' </summary>
        ''' <param name="FrequencyKhz"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetSegmentFromFrequency(ByVal FrequencyKhz As Integer) As BandSegment
            For Each item As Generic.KeyValuePair(Of Integer, BandSegment) In s_Segments
                If FrequencyKhz >= item.Value.LowerFrequency And FrequencyKhz <= item.Value.UpperFrequency Then
                    Return item.Value
                End If
            Next

            'Couldn't find the frequency
            Return Nothing
        End Function

#End Region

#Region " Private Shared Methods "

        ''' <summary>
        ''' Generates the Segments
        ''' 
        ''' 1750 - Start of 25Khz Steps
        ''' 2150 - Start of 50Khz Steps
        ''' 5000 - Start of 100Khz Steps
        ''' 15000 - Start of 150Khz Steps
        ''' 21900 - Start of 200Khz Steps
        ''' 22500 - Start of 250Khz Steps
        ''' 26500 - Start of 300Khz Steps
        ''' 30000 - End of Range
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared Sub GenerateSegments()

            s_Segments = New Generic.Dictionary(Of Integer, BandSegment)

            Dim StartFrequency As Integer = 1750

            Dim EndFrequency As Integer = 0
            Dim StepSize As Integer
            Dim Counter As Integer = 0

            Do Until EndFrequency = 30000
                'Set the step size
                Select Case StartFrequency
                    Case 1750
                        StepSize = 25
                    Case 2150
                        StepSize = 50
                    Case 5000
                        StepSize = 100
                    Case 15000
                        StepSize = 150
                    Case 21900
                        StepSize = 200
                    Case 22500
                        StepSize = 250
                    Case 26500
                        StepSize = 300
                End Select

                EndFrequency = StartFrequency + StepSize

                If EndFrequency > 30000 Then
                    EndFrequency = 30000
                End If

                s_Segments.Add(Counter, New BandSegment(Counter, StartFrequency, EndFrequency))

                Counter += 1
                StartFrequency = EndFrequency
            Loop

        End Sub

#End Region

    End Class

End Namespace